layui.use(['form','jquery','layer','table','laydate'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    var laydate = layui.laydate;
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    //刷新按钮
    $('#refresh').click(function () {
        location.reload();
    });
    //返回按钮
    $('#goBack').click(function () {
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if('后台人员解封日志' == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    getDepts();
    getJsUnsealLogs();
    //注册时间渲染
    laydate.render({
        elem: '#endDate',
        type: 'datetime'
    })
    //根据账号名称、所在部门、解封日期查找解封日志
    form.on('submit(search)',function (obj) {
        getJsUnsealLogs(obj.field);
        return false;
    });
    //查询所有部门
    function getDepts() {
        $.ajax({
            url: '/SyDept/getDepts',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var option = data.data;
                var deptId = $('#dept').val();
                for (var i = 0; i < option.length; i++) {
                    if(option[i].id != 0){
                        $('#deptId').append('<option value="' + option[i].id + '">' + option[i].deptName + '</option>');
                    }
                }
                $('#deptId').val(deptId);
                form.render();
            }
        });
    }
    //根据账号名称、所在部门、解封日期查找解封日志
    function getJsUnsealLogs(params){
        $.ajax({
            url: '/JsUnseal/getJsUnsealLogs',
            data: params,
            dataType: 'json',
            success: function (data) {
                jsUnsealLogTableRender(data.data.data);
            }
        });
    }
    //将查询出来的数据渲染到表中
    function jsUnsealLogTableRender(data) {
        table.render({
            elem: '#userDeblockingLogTable',
            title: '后台人员解封日志',
            data: data,
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                first:"首页",
                last:"尾页",
            },
            width:1630,
            cols: [[
                {type:'checkbox',align:'center',width:150},
                {title:'序号',align:'center',width:160,templet:function(data){
                    return data.LAY_INDEX;
                }},
                {field:"account",title:"账号名称",align:'center',width:200},
                {field:"deptName",title:"部门名称",align:'center',width:240},
                {field:"days",title:"封停天数",align:'center',width:200,templet:function(data){
                    return data.days == 36500 ? '100年' : data.days + '天';
                }},
                {field:"type",title:"用户类型",align:'center',width:240,templet:function(data){
                    if(data.type == 3){
                        return "后台用户";
                    }
                }},
                {field:"endDate",title:"解封时间",align:'center',width:280},
                {field:'',title:"操作",align:'center',width:150,toolbar: '#operation'}
            ]]
        });
        table.on('tool(userDeblockingLogTable)',function (obj) {
            if(obj.event == 'selectTable'){
                layer.open({
                    type: 2,
                    title: '查看用户信息',
                    area:["95%","99%"],
                    scrollbar: false,
                    offset:'auto',
                    btn: ['确定','关闭'],
                    content: '/jurisdiction/backgroundPersonnelManagement/backstageStaff/select-user-message.html',
                    yes: function (index, layero) {
                        layer.close(index);
                    },
                    success: function (layero, index) {
                        var childHtml = layero.find('iframe').contents();
                        $.ajax({
                            url: '/login/getUserById',
                            data: {id: obj.data.userCode},
                            dataType: 'json',
                            success: function (data) {
                                var user = data.data;
                                childHtml.find('#acatarUrl').attr('src',user.acatarUrl);
                                childHtml.find('.trueName').text(user.trueName);
                                childHtml.find('#account').text(user.account);
                                childHtml.find('#trueName').text(user.trueName);
                                childHtml.find('#sex').text(user.sex);
                                childHtml.find('#phone').text(user.phone);
                                childHtml.find('#idCard').text(user.idCard);
                                childHtml.find('#birthDate').text(user.birthDate);
                                childHtml.find('#deptName').text(user.deptName);
                                childHtml.find('#registerTime').text(user.registerDate);
                                childHtml.find('#registerIp').text(user.registerIp);
                                childHtml.find('#email').text(user.email);
                                childHtml.find('#freestyle').text(user.freestyle);
                            }
                        });
                    }
                })
            }
        });
    }
})